﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XiTongRoles.aspx.cs" Inherits="PropertyAppraisal.SystemManage.XiTongRoles" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>角色管理</title>

    <link href="../jquery-easyui-1.3.5/themes/default/easyui.css" rel="stylesheet" />
   <link href="../easyui/easyui/1.3.2/themes/icon.css" rel="stylesheet" />
    <script type="text/javascript" src="../jquery-easyui-1.3.5/jquery-1.7.2-min.js"></script>
    <script type="text/javascript" src="../jquery-easyui-1.3.5/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../jquery-easyui-1.3.5/easyloader.js"></script>
    <script src="../easyui/easyui/1.3.2/locale/easyui-lang-zh_CN.js"></script>

    <script type="text/javascript" src="../easyui/jquery.params.js"></script>
</head>
<body>
    <div>
            <iframe width="0" height="0" src="../SessionKeeper.aspx"></iframe>   
        <%-- 模态窗 --%>
        <div style="margin: 20px 0;">
            <%--<a href="javascript:void(0) " class="easyui-linkbutton" onclick="addAuth()">分配权限</a>--%>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#w').window('open')">添加角色</a>
            <%--	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#w').window('close')">Close</a>--%>
        </div>
        <%-- 添加角色--%>
        <div id="w" class="easyui-window" title="添加角色" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width: 500px; height: 200px; padding: 10px;">
            <form id="form1" runat="server">
                <table>
                    <tr>
                        <td>角色名称：</td>
                        <td>
                           <input id="name" class="easyui-validatebox" missingMessage="必填" type="text" name="linkmen" data-options="required:true" style="width: 800px;" placeholder="请输入角色名称" maxlength="20" />
                        </td>
                    </tr>
                    <tr>
                        <td>备注：</td>
                        <td>
                            <input id="beizhu" class="easyui-textbox" data-options="multiline:true"  type="text" name="linkmen"  style="width: 800px;"  placeholder="请输入备注" maxlength="200" />
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <a href="javascript:void(0)" class="easyui-linkbutton" id="btn" ">提交</a> </td>
                    </tr>
                </table>
            </form>
        </div>
        <%--   分配权限--%>
        <%-- <div id="trees" class="easyui-window" title="权限分配" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width: 500px; height: 200px; padding: 10px;">
            <ul id="tt"></ul>
        </div>--%>
        <table id="ta" title="角色列表" style="width: 900px;" data-options="
				singleSelect:true,
				autoRowHeight:false,
				pagination:true,
				pageSize:10">
            <thead>
                <tr>
                    <th field="chek" width="50"></th>
                    <th field="inv" width="100">角色</th>
                    <th field="date" width="200">备注</th>
                    <th field="name" width="100">是否启用</th>
                    <th field="amount" width="200">操作</th>
                </tr>
            </thead>
        </table>
    </div>



    <script>

        //跳转权限分配
        function addAuth() {
            var chek = "";
            $(":checkbox:checked").each(function () {
                chek += $(this).val();
            })
            var roleid = chek;
            var firstval = $(":checkbox:checked").first().val();
            if (chek.length != firstval.length) {
                $.messager.alert("", "每次只能操作一个角色！");
            }
            else {
                var role = $(":checkbox:checked").parent().parent().parent().children().eq(1).children().html();
                parent.addTab("分配权限", "/SystemManage/XiTongAuthtority.aspx?id=" + roleid + "&name=" + role, "icon-chart-organisation", true);
            }
        }

        //表格
        function getData() {
            var rows = [];
            $.ajax({
                type: "post", //要用post方式                 
                url: "XiTongRoles.aspx/showRole",//方法所在页面和方法名
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function (result) {
                    var json = eval("(" + result.d + ")");
                    for (var i = 0; i < json.length; i++) {
                        rows.push({
                            chek: '<input type="checkbox" value="' + json[i].roles_Id + '"/>',
                            inv: json[i].roles_Name,
                            date: json[i].roles_Remarks,
                            name: json[i].roles_IsState == 0 ? "启用" : "未启用",
                            amount: '<input type="button" onclick="delrole(this,' + json[i].roles_Id + ')" value="删除"/><input type="button" onclick="uptrole(' + json[i].roles_Id + ')"  value="修改"/><input type="button" value="停用" onclick="ting(this,' + json[i].roles_Id + ')"/><input type="button" value="启用" onclick="qi(this,' + json[i].roles_Id + ')"/>',
                        });
                    }
                }
            })
            return rows;
        }

        //停用角色
        function ting(obj, id) {
            var zhuang = $(obj).parent().parent().parent().children().eq(3).children().html();
            if (zhuang == "未启用") {
                $.messager.alert("", "操作失败！该角色原本已被停用");
                return;
            }
            else {
                $.ajax({
                    type: "post",
                    url: "XiTongRoles.aspx/StopRole",
                    data: "{'roleid':'" + id + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function (result) {
                        var json = eval("(" + result.d + ")");

                        if (json == 1) {
                            $.messager.alert("", "操作成功！");
                            $(obj).parent().parent().parent().children().eq(3).children().html("未启用");
                        }
                        else {
                            $.messager.alert("", "操作失败！");
                        }
                    }
                })
            }
        }

        //启用角色
        function qi(obj, id) {
            var zhuang = $(obj).parent().parent().parent().children().eq(3).children().html();
            if (zhuang == "启用") {
                $.messager.alert("", "操作失败！该角色原本已被启用");
                return;
            }
            else {
                $.ajax({
                    type: "post",
                    url: "XiTongRoles.aspx/StartRole",
                    data: "{'roleid':'" + id + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function (result) {
                        var json = eval("(" + result.d + ")");

                        if (json == 1) {
                            $.messager.alert("", "操作成功！");
                            $(obj).parent().parent().parent().children().eq(3).children().html("启用");
                        }
                        else {
                            $.messager.alert("", "操作失败！");
                        }
                    }
                })
            }
        }


        //删除角色
        function delrole(obj, id) {    
            $.messager.defaults = { ok: "是", cancel: "否" };
            $.messager.confirm('删除提示', '确定删除吗?', function (data) {
                if (data) {
                    $.ajax({
                        type: "post",
                        url: "XiTongRoles.aspx/DelRole",
                        data: "{'roleid':'" + id + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        async: false,
                        success: function (result) {
                            var json = eval("(" + result.d + ")");

                            if (json == 1) {
                                $.messager.alert("", "删除成功！");
                                $(obj).parent().parent().parent().remove();
                            }
                            else {
                                $.messager.alert("", "删除失败！");
                            }
                        }
                    })
                }
                else {
                    $.messager.alert("提示", "取消操作！");
                }
            })    
        }

        //修改角色
        function uptrole(id) {
            parent.addTab("修改角色", "/SystemManage/UpdateRole.aspx?id=" + id, "icon-chart-organisation", true);
        }
        //添加角色
        $("#btn").bind('click', function () {
            var name = $("#name").val();
            var beizhu = $("#beizhu").val();
            $.ajax({
                type: "post", //要用post方式                 
                url: "XiTongRoles.aspx/AddRole",//方法所在页面和方法名
                contentType: "application/json; charset=utf-8",
                data: "{ 'name': '" + name + "','beizhu': '" + beizhu + "' }",
                dataType: "json",
                async: false,
                success: function (result) {
                    if(result)
                        $.messager.alert("提示", "添加成功");
                    else
                        $.messager.alert("提示", "添加失败");
                }
            })
        })
        function pagerFilter(data) {
            if (typeof data.length == 'number' && typeof data.splice == 'function') {	// is array
                data = {
                    total: data.length,
                    rows: data
                }
            }
            var dg = $(this);
            var opts = dg.datagrid('options');
            var pager = dg.datagrid('getPager');
            pager.pagination({
                onSelectPage: function (pageNum, pageSize) {
                    opts.pageNumber = pageNum;
                    opts.pageSize = pageSize;
                    pager.pagination('refresh', {
                        pageNumber: pageNum,
                        pageSize: pageSize
                    });
                    dg.datagrid('loadData', data);
                },
                onRefresh: function (pageNum, pageSize) {
                    $('#ta').datagrid({ loadFilter: pagerFilter }).datagrid('loadData', getData());
                }
            });
            if (!data.originalRows) {
                data.originalRows = (data.rows);
            }
            var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
            var end = start + parseInt(opts.pageSize);
            data.rows = (data.originalRows.slice(start, end));
            return data;
        }
        

        $(function () {


            $('#ta').datagrid({ loadFilter: pagerFilter }).datagrid('loadData', getData());
        });


    </script>
</body>
</html>
